package com.andnnl.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * http helper
 * default encode gbk
 * @author chenss
 * 2019��7��2��
 */
public class HttpHelper {

	public static String get(String strURL) {
		return get(strURL, "GBK");
	}

	/**
	 * HTTP GET
	 * @param strURL
	 * @return
	 */
	public static String get(String strURL, String encoding) {
		HttpURLConnection urlConn = null;
		InputStream in = null;
		StringBuffer sb = new StringBuffer();

		System.out.println("HTTP GET:" + strURL);
		try {
			URL url = new URL(strURL);

			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("GET");
			//            urlConn.getOutputStream().flush();
			//            urlConn.getOutputStream().close();
			urlConn.connect();
			in = urlConn.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
			String line = rd.readLine();
			while (line != null) {
				line = line.trim();
				//
				if (!line.equals(""))
					sb.append(line);
				line = rd.readLine();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}

			if (urlConn != null) {
				urlConn.disconnect();
			}
		}

		return sb.toString();
	}

	/**
	 * HTTP GET
	 * @param strURL
	 * @return
	 */
	public static String get2(String strURL, String encoding) {
		HttpURLConnection urlConn = null;
		InputStream in = null;
		StringBuffer sb = new StringBuffer();

		System.out.println("HTTP GET:" + strURL);
		try {
			URL url = new URL(strURL);

			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("GET");
			//            urlConn.getOutputStream().flush();
			//            urlConn.getOutputStream().close();
			urlConn.connect();
			in = urlConn.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
			String line = rd.readLine();
			while (line != null) {
				line = line.trim();
				//
				if (!line.equals(""))
					sb.append(line);
				line = rd.readLine();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}

			if (urlConn != null) {
				urlConn.disconnect();
			}
		}

		return sb.toString();
	}

	public static String post(String strURL, String strPostData) {
		return post(strURL, strPostData, "gbk");
	}

	/**
	 * HTTP POST
	 * @param strURL
	 * @return
	 */
	public static String post(String strURL, String strPostData, String encoding) {
		HttpURLConnection urlConn = null;
		InputStream in = null;
		StringBuffer sb = new StringBuffer();

		System.out.println("HTTP POST:"+strURL);
		System.out.println("HTTP POST Data:"+strPostData);
		try {
			URL url = new URL(strURL);

			urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setRequestMethod("POST");

			OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream(), encoding);
			out.write(strPostData);
			out.flush();
			out.close();

			in = urlConn.getInputStream();
			BufferedReader rd = new BufferedReader(new InputStreamReader(in, encoding));
			String line = rd.readLine();
			boolean isFirst = true;
			while (line != null) {
				if (isFirst) {
					isFirst = false;
				} else {
					sb.append("\n");
				}
				line = line.trim();
				if (!line.equals(""))
					sb.append(line);
				line = rd.readLine();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}

			if (urlConn != null) {
				urlConn.disconnect();
			}
		}

		return sb.toString();
	}

	/**
	 *  save to file
	 * @param    sRemoteHttpURL
	 * @param    sLocalSaveFile
	 * @return   boolean
	 */
	public boolean saveToFile(String sRemoteHttpURL, String sLocalSaveFile) {
		if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals("")) {
			return false;
		}

		try {
			URL tURL = new URL(sRemoteHttpURL);
			HttpURLConnection tHttpURLConnection = (HttpURLConnection) tURL.openConnection();
			tHttpURLConnection.connect();
			BufferedInputStream tBufferedInputStream = new BufferedInputStream(tHttpURLConnection.getInputStream());
			FileOutputStream tFileOutputStream = new FileOutputStream(sLocalSaveFile);

			int nBufferSize = 1024 * 5;
			byte[] bufContent = new byte[nBufferSize];
			int nContentSize = 0;
			while ((nContentSize = tBufferedInputStream.read(bufContent)) != -1) {
				tFileOutputStream.write(bufContent, 0, nContentSize);
			}

			tFileOutputStream.close();
			tBufferedInputStream.close();
			tHttpURLConnection.disconnect();

			tURL = null;
			tHttpURLConnection = null;
			tBufferedInputStream = null;
			tFileOutputStream = null;
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("HttpDownload.saveToFile():"+ex.getMessage());
			System.out.println("sRemoteHttpURL:" + sRemoteHttpURL);
			System.out.println("sLocalSaveFile:" + sLocalSaveFile);
			return false;
		}

		return true;
	}

	public static void main(String args[]) {
//		String s =get("http://zp.chinacnd.com/framework/recruitment/externallinks/valid_sendVaildCode.action?token=MTUzNjExNDI1NzU4Nw==&phone=18589025717","UTF-8");
		String s =get("https://www.22mt.com/wandaoshendilichenyuan/16970320/");
		System.out.println(s);
	}
}
